﻿using System.Windows;
using ff14bot.Enums;
using ff14bot.Managers;
using TreeSharp;
using Magitek.Helpers;
using ff14bot;
using ff14bot.Objects;
using ff14bot.Behavior;

namespace Magitek.Rotations
{
    public class WhiteMage : Magitek
    {
        public override float PullRange { get { return WindowSettings.WhiteMagePullRange; } }
        public override ClassJobType[] Class { get { return new ClassJobType[] { ClassJobType.WhiteMage }; } }

        #region Pulling & Resting
        protected override Composite CreatePull()
        {
            return Combat.CreatePull(25);
        }

        protected override Composite CreateRest()
        {
            return Extensions.DefaultRestBehavior(r => Core.Player.CurrentManaPercent);
        }
        #endregion
        #region Buffs // Mainly used for Grindbot etc...
        protected override Composite CreatePreCombatBuffs()
        {
            return new PrioritySelector(
                Extensions.SummonChocobo()
                );
        }

        protected override Composite CreateCombatBuffs()
        {
            return new PrioritySelector(

            );
        }
        #endregion
        #region Combat
        protected override Composite CreateHeal()
        {
            return new PrioritySelector(
                PartyHelper.AcquireTargetInformation(),

                new Decorator(ret =>
                {
                    if (!WindowSettings.WhiteMageSwiftcastRez)
                        return false;

                    if (!Core.Player.HasAura("Swiftcast"))
                        return false;

                    return true;

                }, new PrioritySelector(

                    Casting.Cast(r => "Raise", r => WindowSettings.WhiteMageSwiftcastRez && Core.Player.InCombat && PartyHelper.Deadtarget != null && Core.Player.HasAura("Swiftcast") && Core.Player.InCombat, r => PartyHelper.Deadtarget, r => 3, r => false)

                    )),

                new Decorator(ret =>
                {

                    if (!PartyManager.IsInParty)
                        return false;

                    return true;

                },

                new PrioritySelector(
                Combat.DoPVPHealing(),

                Casting.Cast(r => "Shroud of Saints", r => Core.Player.CurrentManaPercent <= WindowSettings.WhiteMageShroudOfSaintsMp, r => Core.Player, r => 3, r => false),
                Casting.Cast(r => "Presence of Mind", r => PartyHelper.Healtarget != null && Actionmanager.CanCast("Cure", PartyHelper.Healtarget) && PartyHelper.Healtarget.CurrentHealthPercent < 40, r => Core.Player, r => 3, r => false),
                Casting.Cast(r => "Divine Seal", r => PartyHelper.Healtarget != null && Actionmanager.CanCast("Cure", PartyHelper.Healtarget) && PartyHelper.Healtarget.CurrentHealthPercent < 40, r => Core.Player, r => 3, r => false),
                Casting.Cast(r => "Raise", r => PartyHelper.Deadtarget != null && !Core.Player.InCombat, r => PartyHelper.Deadtarget, r => 10, r => false),
                Casting.Cast(r => "Raise", r => WindowSettings.WhiteMageSwiftcastRez && Core.Player.InCombat && PartyHelper.Deadtarget != null && Core.Player.HasAura("Swiftcast") && Core.Player.InCombat, r => PartyHelper.Deadtarget, r => 3, r => false),
                Casting.Cast(r => "Benediction", r => Core.Player.InCombat && PartyHelper.Healtarget != null && PartyHelper.Healtarget.CurrentHealthPercent < 15, r => PartyHelper.Healtarget, r => 3, r => false),
                Casting.Cast(r => "Presence of Mind", r => Core.Player.InCombat && PartyHelper.Healtarget != null && Actionmanager.CanCast("Cure", PartyHelper.Healtarget) && PartyHelper.Healtarget.CurrentHealthPercent < 25, r => Core.Player, r => 3, r => false),
                Casting.Cast(r => "Esuna", r => PartyHelper.Dispeltarget != null, r => PartyHelper.Dispeltarget, r => 3, r => false),
                Casting.Cast(r => "Swiftcast", r => WindowSettings.WhiteMageSwiftcastRez && Core.Player.InCombat && PartyHelper.Deadtarget != null, r => Core.Player, r => 4, r => false),
 
                Casting.Cast(r => "Medica II", r => PartyHelper.PartyMembersNeedingHealsMedicaII(WindowSettings.WhiteMageMedicaIIHp) >= WindowSettings.TargetsToAOEHeal, r => Core.Player, r => 5, r => true),
                Casting.Heal(r => "Medica", r => PartyHelper.PartyMembersNeedingHeals(WindowSettings.WhiteMageMedicaHp, 15) >= WindowSettings.TargetsToAOEHeal, r => Core.Player, r => 5, r => true),

                //   Group Healing
                Casting.Heal(r => "Cure III", r => PartyHelper.Healtarget != null && PartyHelper.PeopleNeedingHealsAroundTarget(WindowSettings.WhiteMageCureIIIOvercureHp, PartyHelper.Healtarget) >= WindowSettings.WhiteMageTargetsToCureIII && Core.Player.HasAura("Overcure"), r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Heal(r => "Cure III", r => PartyHelper.Healtarget != null && PartyHelper.PeopleNeedingHealsAroundTarget(WindowSettings.WhiteMageCureIIIHp, PartyHelper.Healtarget) >= WindowSettings.WhiteMageTargetsToCureIII, r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Heal(r => "Cure II", r => PartyHelper.Healtarget != null && PartyHelper.IsTank(PartyHelper.Healtarget) && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageTankCureHp && Core.Player.HasAura("Freecure"), r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Heal(r => "Cure II", r => PartyHelper.Healtarget != null && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageCureHp && Core.Player.HasAura("Freecure"), r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Heal(r => "Cure II", r => PartyHelper.Healtarget != null && PartyHelper.IsTank(PartyHelper.Healtarget) && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageTankCureIIHp, r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Heal(r => "Cure II", r => PartyHelper.Healtarget != null && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageCureIIHp, r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Cast(r => "Regen", r => Core.Player.InCombat && PartyHelper.Healtarget != null && PartyHelper.IsTank(PartyHelper.Healtarget) && !PartyHelper.Healtarget.HasAura("Regen", true) && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageTankRegenHp, r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Cast(r => "Regen", r => Core.Player.InCombat && PartyHelper.Healtarget != null && !PartyHelper.Healtarget.HasAura("Regen", true) && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageRegenHp, r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Heal(r => "Cure", r => PartyHelper.Healtarget != null && PartyHelper.IsTank(PartyHelper.Healtarget) && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageTankCureHp, r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Heal(r => "Cure", r => PartyHelper.Healtarget != null && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageCureHp, r => PartyHelper.Healtarget, r => 1, r => true),
                Casting.Cast(r => "Protect", r => WindowSettings.WhiteMageProtectOOC && !Core.Player.InCombat && PartyHelper.Protecttarget != null, r => PartyHelper.Protecttarget, r => 3, r => true),
                Casting.Cast(r => "Stoneskin", r => WindowSettings.WhiteMageStoneskinOOC && !Core.Player.InCombat && PartyHelper.Stoneskintarget != null, r => PartyHelper.Stoneskintarget, r => 3, r => true),


                //   Alliance Healing if enabled.
                Casting.Heal(r => "Cure II", r => WindowSettings.HealEveryone && PartyHelper.Alliancehealtarget != null && PartyHelper.Alliancehealtarget.CurrentHealthPercent <= WindowSettings.WhiteMageCureHp && Core.Player.HasAura("Freecure"), r => PartyHelper.Alliancehealtarget, r => 1, r => true),
                Casting.Heal(r => "Cure II", r => WindowSettings.HealEveryone && PartyHelper.Alliancehealtarget != null && PartyHelper.Alliancehealtarget.CurrentHealthPercent <= WindowSettings.WhiteMageCureIIHp, r => PartyHelper.Alliancehealtarget, r => 1, r => true),
                Casting.Cast(r => "Regen", r => WindowSettings.HealEveryone && PartyHelper.Alliancehealtarget != null && PartyHelper.Healtarget.CurrentHealthPercent <= WindowSettings.WhiteMageRegenHp && !PartyHelper.Alliancehealtarget.HasAura("Regen"), r => PartyHelper.Alliancehealtarget, r => 1, r => true),
                Casting.Heal(r => "Cure", r => WindowSettings.HealEveryone && PartyHelper.Alliancehealtarget != null && PartyHelper.Alliancehealtarget.CurrentHealthPercent <= WindowSettings.WhiteMageCureHp, r => PartyHelper.Alliancehealtarget, r => 1, r => true))),
 
                new Decorator(ret =>
                {

                    if (!Core.Player.HasTarget)
                        return false;

                    if (!Core.Player.InCombat)
                        return false;
                     
                    if (!Core.Player.CurrentTarget.CanAttack)
                        return false;

                    if ((WindowSettings.WhiteMageDoDPS && Core.Player.CurrentManaPercent >= WindowSettings.WhiteMageStopDPSMana) || !PartyManager.IsInParty || WindowSettings.SoloMode)
                        return true;

                    return false;

                }, CreateCombat()),
                
                Casting.Cast(r => "Esuna", r => PartyHelper.Dispeltarget != null, r => PartyHelper.Dispeltarget, r => 3, r => false),
                Casting.Cast(r => "Cleric Stance", r => (WindowSettings.SoloMode && !Core.Player.HasAura("Cleric Stance")) || (!WindowSettings.SoloMode && Core.Player.HasAura("Cleric Stance")), r => Core.Player, r => 3),
                Casting.Cast(r => "Protect", r => WindowSettings.WhiteMageProtectOOC && !Core.Player.InCombat && !Core.Player.HasAura("Protect"), r => Core.Player, r => 3, r => true),
                Casting.Cast(r => "Stoneskin", r => WindowSettings.WhiteMageStoneskinOOC && !Core.Player.InCombat && !Core.Player.HasAura("Stoneskin"), r => Core.Player, r => 3, r => true),
                Casting.Cast(r => "Regen", r => !Core.Player.HasAura("Regen", true) && Core.Player.CurrentHealthPercent <= WindowSettings.WhiteMageRegenHp, r => Core.Player, r => 3, r => true),
                Casting.Heal(r => "Cure", r => Core.Player.CurrentHealthPercent < WindowSettings.WhiteMageCureHp, r => Core.Player, r => 3, r => true));
        }

        protected override Composite CreateCombat()
        {
            return new PrioritySelector(ctx => Core.Player.CurrentTarget,
                new Decorator(ctx => ctx != null,
                new PrioritySelector(
                new Decorator(r => BotManager.Current.IsAutonomous, new PrioritySelector(
                CommonBehaviors.MoveToLos(ctx => ctx as GameObject),
                CommonBehaviors.MoveAndStop(ctx => (ctx as GameObject).Location, ctx => 20, true, "[Magitek] Moving in Range"))),

                Casting.Cast(r => "Cleric Stance", r => WindowSettings.ToggleClericStance && !Core.Player.HasAura("Cleric Stance"), r => Core.Player, r => 2, r => false),

                new Decorator(ret =>
                {
                    if (!Core.Player.HasTarget)
                        return false;

                    if (!Core.Player.InCombat)
                        return false;

                    if (!Core.Player.CurrentTarget.CanAttack)
                        return false;

                    if ((WindowSettings.WhiteMageDoDPS && Core.Player.CurrentManaPercent >= WindowSettings.WhiteMageStopDPSMana) || !PartyManager.IsInParty || WindowSettings.SoloMode)
                        return true;

                    return false;

                }, new PrioritySelector(

                    Combat.DoPVP(),
                    Casting.Cast(r => "Aero II", r => !Core.Player.CurrentTarget.HasAura("Aero II", true), r => Core.Player.CurrentTarget, r => 3, r => true),
                    Casting.Cast(r => "Aero", r => !Core.Player.CurrentTarget.HasAura("Aero", true), r => Core.Player.CurrentTarget, r => 3, r => true),
                    Casting.Cast(r => "Stone II", r => true, r => Core.Player.CurrentTarget, r => 3),
                    Casting.Cast(r => "Stone", r => true, r => Core.Player.CurrentTarget, r => 3)

               )))));
        }
        #endregion
    }
}